package org.example.easybuy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.easybuy.dao.ProductDao;
import org.example.easybuy.dao.ProductElasticsearch;
import org.example.easybuy.entity.Category;
import org.example.easybuy.entity.Product;
import org.example.easybuy.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductDao productDao;

    @Autowired
    private ProductElasticsearch elasticsearch;
    @Override
    public PageInfo<Product> getProductPageInfo(Map<String, Object> params) {
        params.put("orderBy","creationDate desc");
        PageHelper.startPage(params);
        List<Product> list=productDao.selectProductList(params);
        return new PageInfo<>(list,3);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public boolean deleteProduct(String id) throws Exception {
        try{
            //1、先删除数据库
            if(productDao.deleteProduct(id)==1)
            {
//                int x=10/0;
                //2、再删除ES
                elasticsearch.deleteById(id);
            }

        }catch(Exception e){
            throw new Exception();
        }
        return true;
    }

    @Override
    public Product selectById(String id) {
        return productDao.selectById(id);
    }

    @Override
    public List<Category> getUpdateLevel(int type,int id) {
        return productDao.getUpdateLevel(type,id);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public boolean updateProduct(Product product) throws Exception {
        try{
            if(productDao.updateProduct(product)==1)
            {
                Product p=elasticsearch.findById(product.getId()).get();
                p.setName(product.getName());
                p.setFileName(product.getFileName());
                p.setDescription(product.getDescription());
                p.setPrice(product.getPrice());
                p.setStock(product.getStock());
                elasticsearch.save(p);
            }
        }catch(Exception e)
        {
            throw new Exception();
        }
        return true;
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public boolean addProduct(Product product) throws Exception {
        try{
            if(productDao.addProduct(product)==1)
            {
//                Thread.sleep(1000);
//                Product p=productDao.selectByNameAndDate(product);
//                product.setId(p.getId());
                elasticsearch.save(product);
            }
        }catch(Exception e)
        {
            throw new Exception();
        }
        return true;
    }

    @Override
    public Product checkProductName(Map<String, Object> params) {
        return productDao.checkProductName(params);
    }

    @Override
    public List<Product> selectProductsById(int id) {
        return productDao.selectProductsById(id);
    }

    @Override
    public boolean updateStock(Product product) {
        return productDao.updateStock(product)==1;
    }


}
